<?php

namespace app\admin\model;

use app\common\model\TimeModel;
use ClassLibrary\ClHttp;

class Calculation extends TimeModel {

    protected $name = "calculation";

    protected $deleteTime = "delete_time";


    public function calculationCompany() {
        return $this->belongsTo('\app\admin\model\CalculationCompany', 'calculation_company_id', 'id');
    }

    public function calculationCurrency() {
        return $this->belongsTo('\app\admin\model\CalculationCurrency', 'calculation_currency_id', 'id');
    }


    public function getTypeList() {
        return ['1' => '现货', '2' => '期货',];
    }

    public function getStatusList() {
        return ['1' => '启用', '2' => '禁用',];
    }

    /**
     *判断库存是否充足
     * @param $number
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/7 上午10:03
     */
    public function isStockNumber($number) {
        $where   = [];
        $where[] = ['stock', '>', 0];
        $where[] = ['status', '=', 1];
        $stock   = self::instance()->where($where)->find()->toArray();
        if (empty($stock)) {
            return false;
        }
        if ($stock < $number) {
            return false;
        }

        return true;
    }

    /**
     * 扣除库存
     * @param $all
     * @return mixed
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/9 下午12:09
     */
    public function decProductStock($all) {
        return self::instance()->where([
            ['id', '=', $all['type_id']],
            ['status', '=', 1],
            ['stock', '>=', $all['number']],
        ])->dec('stock', $all['number'])->update();
    }

    public function add_user_coin_address() {
        $params['appid']  = config('pay')['appid'];
        $params['method'] = 'market';
        $sign             = zt_pay_sign($params);
        $params['sign']   = $sign;
        $itmes = ClHttp::request(config('pay')['url'], $params, 'json');
        return $itmes;
    }

}